package com.gxpt.app;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.gxpt.app.bean.login_bean;
import com.gxpt.app.view.Activity_city_select_2;
import com.utils.SPUtils;
import com.utils.SysUtils;
import com.utils.logger.Logger;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017-2-13.
 */

public class AppUtils {

    public static void putXML_token(String token, Context context){
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_Name,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Constant.XML_token,     token);
        editor.commit();
    }

    public static void putXML_user(login_bean.login_info user, Context context){
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_Name,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Constant.XML_token,            user.token);
        editor.putInt(Constant.XML_userID,              user.id);
        editor.putString(Constant.XML_nickname,         user.mem_nickname);
        editor.putString(Constant.XML_realname,         user.mem_realname);
        editor.putInt(Constant.XML_sex,                 user.mem_sex);
        editor.putInt(Constant.XML_level,               user.mem_level);
        editor.putString(Constant.XML_card_photos,      user.mem_idcard_photo);
        editor.putString(Constant.XML_card_fphotos,     user.mem_idcard_fphoto);
        editor.putString(Constant.XML_headport,         user.mem_headportrai);
        editor.putString(Constant.XML_tel,              user.mem_tel);
        editor.commit();
    }

    public static void putXML_user_exit(Context context){
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_Name,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(Constant.XML_userID,              0);
        editor.putString(Constant.XML_nickname,        "");
        editor.putString(Constant.XML_realname,         "");
        editor.putInt(Constant.XML_sex,                 0);
        editor.putInt(Constant.XML_level,               0);
        editor.putString(Constant.XML_card_photos,      "");
        editor.putString(Constant.XML_card_fphotos,     "");
        editor.putString(Constant.XML_headport,         "");
        editor.putString(Constant.XML_tel,              "");
        editor.commit();
    }

    public static void putXML_area(Context context,String main,String sub,String main_name,String sub_name){
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_Name,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Constant.XML_area_main_name,             main_name);
        editor.putString(Constant.XML_area_main,                  main);
        editor.putString(Constant.XML_area_sub_name,              sub_name);
        editor.putString(Constant.XML_area_sub,                   sub);

        editor.commit();
    }

    /**
     * isShowAreaSel(context)
     * */
    public static void isShowAreaSel(Context context){
        if(AppUtils.get_area_sub_name(context).isEmpty()){
            Bundle b = new Bundle();
            b.putString("FORM_","Fragment_home");
            Intent gotype = new Intent();
            gotype.setClass(context, Activity_city_select_2.class);
            gotype.putExtra("DATA",b);
            context.startActivity(gotype);
        }
    }


    public static boolean isHaveHTTP(String Check_str){
        if(Check_str == null ) return false;
        if(Check_str.indexOf("http")>=0){
            return true;
        }else{
            return false;
        }
    }


    /**
     * SUCCESS—支付成功
     REFUND—转入退款
     NOTPAY—未支付
     CLOSED—已关闭
     REVOKED—已撤销（刷卡支付）
     USERPAYING—用户支付中
     PAYERROR—支付失败(其他原因，如银行返回失败)
     * */
    public static String getWXPay_STUTS_NAME(String str){
        String tmp = "";
        if(str.trim().equals("SUCCESS")){
            tmp =  "支付成功";
        }else if(str.trim().equals("REFUND")){
            tmp =  "转入退款";
        }else if(str.trim().equals("NOTPAY")){
            tmp =  "未支付";
        }else if(str.trim().equals("CLOSED")){
            tmp =  "已关闭";
        }else if(str.trim().equals("REVOKED")){
            tmp =  "已撤销";
        }else if(str.trim().equals("USERPAYING")){
            tmp =  "用户支付中";
        }else if(str.trim().equals("PAYERROR")){
            tmp =  "支付失败";
        }

        return tmp;
    }

    public static boolean getWXPay_STUTS(String str){
        boolean tmp = false;
        if(str.trim().equals("SUCCESS")){
            tmp =  true;
        }else if(str.trim().equals("REFUND")){
            tmp = false;
        }else if(str.trim().equals("NOTPAY")){
            tmp = false;
        }else if(str.trim().equals("CLOSED")){
            tmp = false;
        }else if(str.trim().equals("REVOKED")){
            tmp = false;
        }else if(str.trim().equals("USERPAYING")){
            tmp = false;
        }else if(str.trim().equals("PAYERROR")){
            tmp = false;
        }

        return tmp;
    }


    public static String getInvoice(int index){
        String tmp = "";
        switch (index){//0明细 1耗材 2办公用品3 电脑配件
            case 0:
                tmp =  "明细";
                break;
            case 1:
                tmp =  "耗材";
                break;
            case 2:
                tmp =  "办公用品";
                break;
            case 3:
                tmp =  "电脑配件";
                break;
        }
        return tmp;
    }

    public static String getOrderSTUTS(int index){
        String tmp = "";
        switch (index){
            case 0:
                tmp =  "未支付";
                break;
            case 1:
                tmp =  "待发货";
                break;
            case 2:
                tmp =  "待收货";
                break;
            case 3:
                tmp =  "已完成";
                break;
            case 4:
                tmp =  "已取消";
                break;
                default:
                    tmp =  "未知";
                    break;

        }
        return tmp;
    }

    public static String getApplySTUTS(int index){
        String tmp = "";
        switch (index){
            case 0:
                tmp =  "待处理";
                break;
            case 1:
                tmp =  "已处理";
                break;
            case 2:
                tmp =  "处理中";
                break;
            case 3:
                tmp =  "已完成";
                break;
            case 4:
                tmp =  "已关闭";
                break;
            default:
                tmp =  "未知";
                break;

        }
        return tmp;
    }





    public static String get_area_main_id(Context context){

        return (String) SPUtils.get(context,Constant.XML_area_main,"");
    }

    public static String get_area_sub_id(Context context){

        return (String) SPUtils.get(context,Constant.XML_area_sub,"");
    }

    public static String get_area_sub_name(Context context){

        return (String) SPUtils.get(context,Constant.XML_area_sub_name,"");
    }


    public static int get_user_id(Context context){

        return (int) SPUtils.get(context,Constant.XML_userID,0);
    }


    public static String get_XML_tel(Context context){

        return (String) SPUtils.get(context,Constant.XML_tel,"");
    }

    public static int get_mem_level(Context context){

        return (int) SPUtils.get(context,Constant.XML_level,0);
    }

    public static String get_device_id(Context context){

        return (String) SPUtils.get(context,Constant.device_ID,Constant.device_ID);
    }

    public static String get_date(int dateInt) throws ParseException {
        try{

            String res="";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long time = new Long(dateInt);
            Date date = new Date(time);
            res = simpleDateFormat.format(date);
            return res;
        }catch (Exception e){
            return "";
        }
    }

    public static String get_token(Context context){

        return (String) SPUtils.get(context,Constant.XML_token,"");

    }

    public static void put_token(Context context,String xgToken){

        SPUtils.put(context,Constant.XML_token,""+xgToken);
    }

    public static void put_isFirst(Context context){

        Logger.d("当前版本号："+ SysUtils.getVersionName(context));
        SPUtils.put(context,Constant.XML_isFirst,""+ SysUtils.getVersionName(context));
    }

    public static String getJYMS(String index){
        String tmp = "";
        switch (Integer.parseInt(index)){
            case 0:
                tmp =  "一口价";
                break;
            case 1:
                tmp =  "招标";
                break;
        }
        return tmp;
    }

    public static String getSTUTS(String index){
        String tmp = "";
        switch (Integer.parseInt(index)){
            case 0:
                tmp =  "[未审核]";
                break;
            case 1:
                tmp =  "[已审核]";
                break;
            case 2:
                tmp =  "[已驳回]";
                break;
            case 3:
                tmp =  "[已关闭]";
                break;
        }
        return tmp;
    }

    public static String getJBSTUTS(String index){
        String tmp = "";
        switch (Integer.parseInt(index)){
            case 0:
                tmp =  "待投标";
                break;
            case 1:
                tmp =  "投标中";
                break;
            case 2:
                tmp =  "已中标";
                break;
            case 3:
                tmp =  "待网签";
                break;
            case 4:
                tmp =  "已网签";
                break;
            case 5:
                tmp =  "实施中";
                break;
            case 6:
                tmp =  "已流标";
                break;
            case 7:
                tmp =  "已完成";
                break;
//            case 0:
//                tmp =  "投标中";
//                break;
//            case 1:
//                tmp =  "已中标";
//                break;
//            case 2:
//                tmp =  "待网签";
//                break;
//            case 3:
//                tmp =  "已网签";
//                break;
//            case 4:
//                tmp =  "实施中";
//                break;
//            case 5:
//                tmp =  "已流标";
//                break;
//            case 6:
//                tmp =  "已完成";
//                break;
        }
        return tmp;
    }


    public static int dp2px(Context context, int dp)
    {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }



    /**
     * double 相加
     * @param d1
     * @param d2
     * @return
     */
    public static double double_sum(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.add(bd2).doubleValue();
    }

    public static double double_sub(double d1,double d2){//相减
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }

}
